﻿package net.wangit.view.element.component;

import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.support.AbstractElementWriter;
import net.wangit.util.Checker;
import net.wangit.util.StringUtil;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.util.Html;

public class ButtonBoxElementWriter extends AbstractElementWriter {

	public ButtonBoxElementWriter() {
	}

	protected void writeText(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeTextWithValue(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControlWithDisplay(ac, html, ec, row, false);
	}

	protected void writeReadOnlyControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeDisabledControl(ac, html, ec, row);
	}

	protected void writeDisabledControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		html.append("<span unselectable=on");
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setCss(html, "E41D");
		html.append(">");
		String link = ElementUtil.resolveLink(ac, ec, row);
		if (!Checker.isEmpty(link)) {
			String icons[] = StringUtil.split(link, ",");
			html.append("<img src=\"").append(
					icons.length <= 1 ? icons[0] : icons[1]).append("\"/>");
		}
		String name = ElementUtil.resolveName(ac, ec);
		if (!Checker.isEmpty(link) && !Checker.isEmpty(name))
			html.append("<span unselectable=on class=").append("E41S").append(
					"></span>");
		html.append("<span unselectable=on class=").append("E41DT").append(">");
		Html.set(html, name);
		html.append("</span>");
		html.append("</span>");
	}

	protected void writeHidden(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControlWithDisplay(ac, html, ec, row, true);
	}

	private void writeControlWithDisplay(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row, boolean hidden) {
		html.append("<span id=\"").append(ElementUtil.getAlias(ec))
				.append("\"");
		if (hidden)
			html.append(" style=\"display:none\"");
		String attribute = ElementUtil.resolveCustom(ac, ec, row);
		Html.setAttribute(html, attribute);
		if (ec.isAction() && Checker.isEmpty(attribute))
			html.append(" onclick=\"doSubmit('").append(
					ElementUtil.getActionUrl(ec)).append("')\"");
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setCss(html, "E41");
		html.append(">");
		html.append("<span unselectable=\"on\" class=\"").append("E41N")
				.append("\" onmouseover=\"className='").append("E41O").append(
						"'\" onmouseout=\"className='").append("E41N").append(
						"'\" onmousedown=\"className='").append("E41P").append(
						"'\" onmouseup=\"className='").append("E41O").append(
						"'\">");
		String link = ElementUtil.resolveLink(ac, ec, row);
		if (!Checker.isEmpty(link)) {
			String icons[] = StringUtil.split(link, ",");
			html.append("<img src=\"").append(icons[0]).append("\"/>");
		}
		String name = ElementUtil.resolveName(ac, ec);
		if (!Checker.isEmpty(link) && !Checker.isEmpty(name))
			html.append("<span unselectable=\"on\" class=\"").append("E41S")
					.append("\"></span>");
		html.append("<span unselectable=\"on\" class=\"").append("E41T")
				.append("\">");
		Html.set(html, name);
		html.append("</span>");
		html.append("</span>");
		html.append("</span>");
	}
}
